package com.hiss.basic.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.hiss.basic.client.system.ISysUserRoleService;
import com.hiss.basic.client.system.ISysUserService;
import com.hiss.basic.model.system.domain.SysUserDomain;
import com.hiss.basic.model.system.request.*;
import com.hiss.basic.model.system.response.SysUserQueryResponse;
import com.hiss.basic.starter.core.model.R;
import com.hiss.basic.starter.log.annotation.Log;
import com.hiss.basic.starter.log.enums.BusinessTypeEnum;
import com.mybatisflex.core.paginate.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * 系统用户 controller
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController {

    @Resource
    private ISysUserService sysUserService;

    @Resource
    private ISysUserRoleService sysUserRoleService;

    @SaCheckPermission("sys:user:list")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public R<List<SysUserQueryResponse>> list(SysUserQueryRequest request) {
        List<SysUserQueryResponse> userList = sysUserService.findList(request);
        return R.success(userList);
    }

    @SaCheckPermission("sys:user:list-page")
    @RequestMapping(value = "/list/page", method = RequestMethod.GET)
    public R<Page<SysUserQueryResponse>> listPage(SysUserQueryRequest request) {
        Page<SysUserQueryResponse> userListPage = sysUserService.findPageList(request);
        return R.success(userListPage);
    }

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public R<SysUserDomain> info(String id) {
        SysUserDomain domain = sysUserService.getById(id);
        domain.setPassword(null);
        return R.success(domain);
    }

    @RequestMapping(value = "/role-ids", method = RequestMethod.GET)
    public R<Collection<Long>> roleIds(Long userId) {
        return R.success(sysUserRoleService.findRoleIdListByUserId(userId));
    }

    @SaCheckPermission("sys:user:create")
    @Log(title = "新增用户", businessType = BusinessTypeEnum.INSERT)
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public R<Void> create(@RequestBody SysUserCreateRequest request) {
        sysUserService.create(request);
        return R.success();
    }

    @SaCheckPermission("sys:user:update")
    @Log(title = "更新用户", businessType = BusinessTypeEnum.UPDATE)
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public R<Void> update(@RequestBody SysUserUpdateRequest request) {
        sysUserService.update(request);
        return R.success();
    }

    @RequestMapping(value = "/update/basic", method = RequestMethod.PUT)
    public R<Void> updateBasic(@RequestBody SysUserBasicUpdateRequest request) {
        sysUserService.updateBasic(request);
        return R.success();
    }

    @RequestMapping(value = "/update/password", method = RequestMethod.PUT)
    public R<Void> updatePassword(@RequestBody SysUserPasswordUpdateRequest request) {
        sysUserService.updatePassword(request);
        return R.success();
    }

    @SaCheckPermission("sys:user:password-reset")
    @Log(title = "重置密码", businessType = BusinessTypeEnum.UPDATE)
    @RequestMapping(value = "/password/reset", method = RequestMethod.GET)
    public R<Void> resetPassword(Long id) {
        sysUserService.resetPassword(id);
        return R.success();
    }

    @SaCheckPermission("sys:user:remove")
    @Log(title = "删除用户", businessType = BusinessTypeEnum.DELETE)
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    public R<Void> remove(Long id) {
        sysUserService.remove(id);
        return R.success();
    }
}
